#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[])
{
	FILE *fp = fopen("/proc/net/wireless", "r");
	if(fp) {
		char line[1024];
		char *p = NULL;
		int ret = 0; // 默认无节点，无连接
		int linkQ = 0;
		fgets(line, sizeof(line), fp); // Head 1
		fgets(line, sizeof(line), fp); // Head 2

		p = fgets(line, sizeof(line), fp);
		if(p != NULL) {
			if((p=strstr(line, "wlan0")) != NULL) {
				// 解析Quality link的值, 跳过status
				sscanf(p+strlen("wlan0")+7, "%d.", &linkQ);
				printf("+++ wireless quality link = %d\n", linkQ);
				if(linkQ == 0) {
					ret = 0; // 未连接热点
				}
				else if(linkQ < 10) {
					ret = -1; // 连接异常
				} else if(linkQ > 10) {
					ret = 1;
				}
			} else { // 无节点
				printf("+++ no wlan interface 2\n");
			}
		}
		else {
			printf("+++ no wlan interface 1\n");
		}

		fclose(fp);
		return ret;
	}
	return -1;
}
